import {
  Notify,
  Dialog,
  QSpinnerIos,
  Loading,
  exportFile
} from 'quasar'
import _ from 'lodash'

function getRandomData(datas) {
  return datas[Math.floor(Math.random() * datas.length)]
}

function getRandomRangeInt(minValue, maxValue) {
  return getRandomFloorInt(maxValue) + minValue
}

function getRandomFloorInt(maxValue) {
  return Math.floor(Math.random() * maxValue)
}

function getRandomCeilInt(maxValue) {
  return Math.ceil(Math.random() * maxValue)
}

function resetObj(obj) {
  const objClone = _.clone(obj)
  Object.keys(objClone).forEach((key) => {
    objClone[key] = null
  })
  return objClone
}

function resetArray(array) {
  if (array && array instanceof Array) {
    const arrayClone = _.clone(array)
    arrayClone.splice(0, arrayClone.length)
    return arrayClone
  }
  return array
}

/* notify - begin */
function notifySuccess(message) {
  Notify.create({
    color: 'white',
    textColor: 'positive',
    icon: 'check_circle',
    position: 'top',
    message: message,
    timeout: '1000'
  })
}

function notifyError(message) {
  Notify.create({
    color: 'white',
    textColor: 'negative',
    icon: 'error',
    position: 'top',
    message: message,
    timeout: '1000'
  })
}

function notifyWaring(message) {
  Notify.create({
    color: 'white',
    textColor: 'warning',
    icon: 'warning',
    position: 'top',
    message: message,
    timeout: '1000'
  })
}

function notifyAlert(message) {
  Notify.create({
    color: 'white',
    textColor: 'negative',
    icon: 'add_alert',
    position: 'top',
    message: message,
    timeout: '1000'
  })
}

function notifyInfo(message) {
  Notify.create({
    color: 'white',
    textColor: 'info',
    icon: 'info',
    position: 'top',
    message: message,
    timeout: '1000'
  })
}

/* notify - end */

/* confirm - begin */
function confirm(message, title) {
  if (!title) {
    title = '提示'
  }
  return Dialog.create({
    title: title,
    message: message,
    cancel: {
      label: '取消',
      color: 'warning',
      unelevated: true
    },
    ok: {
      label: '确认',
      color: 'primary',
      unelevated: true
    },
    persistent: true
  })
}

/* alert - end */

/* confirm - begin */
function alert(message, title) {
  if (!title) {
    title = '系统提示'
  }
  return Dialog.create({
    title: title,
    message: message,
    ok: '确认',
    persistent: true
  })
}

/* alert - end */

function getUrlRootPath() {
  return window.location.protocol + '//' + location.host
}

function showLoading(msg) {
  if (!msg || msg === '') {
    msg = '加载中'
  }
  const spinner = QSpinnerIos
  Loading.show({
    spinner,
    spinnerSize: 40,
    spinnerColor: 'blue',
    backgroundColor: 'white',
    message: msg,
    messageColor: 'blue'
  })
}

function currentURL() {
  return window.location.href;
}

function hideLoading() {
  Loading.hide()
}

function exportTable(columns, datas, separator) {
  if (!separator || separator === '') {
    separator = ','
  }
  // naive encoding to csv format
  const content = [columns.map(col => wrapCsvValue(col.label))].concat(
    datas.map(row => columns.map(col => wrapCsvValue(
      typeof col.field === 'function' ?
      col.field(row)
      // eslint-disable-next-line no-void
      :
      row[col.field === void 0 ? col.name : col.field],
      col.format
    )).join(separator))
  ).join('\r\n')

  const status = exportFile(
    'table-export.csv',
    '\uFEFF' + content,
    'text/csv'
  )
  if (status !== true) {
    notifyWaring('浏览器拒绝文件导出')
  }
}

function wrapCsvValue(val, formatFn) {
  // eslint-disable-next-line no-void
  let formatted = formatFn !== void 0 ?
    formatFn(val) :
    val
  // eslint-disable-next-line no-void
  formatted = formatted === void 0 || formatted === null ?
    '' :
    String(formatted)
  formatted = formatted.split('"').join('""')
  return `"${formatted}"`
}

function getURLParameters(url) {
  return url
    .match(/([^?=&]+)(=([^&]*))/g)
    .reduce((a, v) => (
      (a[v.slice(0, v.indexOf("="))] = v.slice(v.indexOf("=") + 1)), a
    ), {});
}

//获取url中的参数
function getUrlParam(name) {
  var path = window.location.href;
  var reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(\\s|&|$)", "i");
  if (reg.test(path)) {
      return decodeURI(RegExp.$2.replace(/\+/g, " "));
  } else {
      return "";
  }
}

function getPageName(page) {
  let pagename = '其他'
  switch (page) {
    case 'pages/index/index':
      pagename = '首页'
      break
    case 'pages/companylist/merchantlist':
      pagename = '商家列表'
      break
    case 'pages/position/position':
      pagename = '品牌'
      break
    case 'pages/user/usercenter':
      pagename = '我的'
      break
    case 'pages/release/release':
      pagename = '发布'
      break
    case 'pagesA/user/merchantdetail':
      pagename = '商家主页'
      break
    case 'pagesA/cooperation/cooperationlist':
      pagename = '合作'
      break
    case 'pagesA/cooperation/cooperationdetail':
      pagename = '合作详情'
      break
    case 'pagesA/login/login':
      pagename = '登录'
      break
    case 'pagesA/news/newslist':
      pagename = '资讯列表'
      break
    case 'pagesA/news/newsdetail':
      pagename = '资讯详情'
      break
    case 'pagesA/news/likepersonlist':
      pagename = '点赞列表'
      break
    case 'pagesA/product/productlist':
      pagename = '产品列表'
      break
    case 'pagesA/product/productdetail':
      pagename = '产品详情'
      break
    case 'pagesA/protocol/Uprotocol':
      pagename = '用户协议'
      break
    case 'pagesA/search/searchlist':
      pagename = '搜索列表'
      break
    case 'pagesA/searchmerchant/searchmerchant':
      pagename = '搜索结果'
      break
    case 'pagesA/user/uindex_edit':
      pagename = '主页编辑'
      break
    case 'pagesB/user/team':
      pagename = '商家团队'
      break
    case 'pagesB/user/my_score':
      pagename = '我的积分'
      break
    case 'pagesC/user/model_poster':
      pagename = '生成海报'
      break
    case 'pagesC/user/model_tel':
      pagename = '联系方式'
      break
    case 'pagesC/user/infor_link':
      pagename = '公众号文章'
      break
  }
  return pagename
}
export default {
  getRandomData,
  getRandomRangeInt,
  getRandomCeilInt,
  getRandomFloorInt,
  getUrlRootPath,
  notifySuccess,
  notifyInfo,
  notifyWaring,
  notifyAlert,
  notifyError,
  confirm,
  alert,
  resetObj,
  resetArray,
  showLoading,
  hideLoading,
  exportTable,
  getPageName,
  currentURL,
  getURLParameters,
  getUrlParam
}
